<?php
require_once drupal_get_path('module', 'node').'/node.pages.inc';

function pdiary_page_user($user=NULL) {
  $username = theme('username', $user);
  if ($user->uid) {
    $result = pager_query(db_rewrite_sql(
        "SELECT n.nid, n.uid FROM {node} n WHERE n.uid = %d AND n.type = 'pdiary' ORDER BY n.created DESC"),
         5, 0, NULL, $user->uid);
    drupal_set_title($username.' bought these');
  }
  else {
    $result = pager_query(db_rewrite_sql(
       "SELECT n.nid, n.uid FROM {node} n WHERE n.type = 'pdiary'"), 5, 0, NULL);
  }
  // show all the diaries the user has.
  $item_list = array();
  while ($row = db_fetch_array($result)) {
    print_r($row);
    $node = node_load($row['nid']);
    $node_user = user_load($node->uid);
    $item_list[] = l($node_user->name.': '.$node->title, 'pdiary/'.$row['uid'].'/'.$node->nid);
  }
  $output .= theme('item_list', $item_list);
  $output .= theme('pager', NULL, 5);
  
  return $output;
}


function pdiary_page_product($user, $node) {
    $result = pager_query(db_rewrite_sql(
        "SELECT p.nid FROM {pdiary} p JOIN {node} n ON p.pid=n.nid WHERE p.pid = %d AND n.uid = %d"),
        5, 0, NULL, $node->nid, $user->uid);
    $nores = TRUE;
    while ($row = db_fetch_array($result)) {
      print_r($row);
      $entry = node_load($row['nid']);
      $output .= node_view($entry);
      $nores = FALSE;
    }
    if ($nores ) {
      return FALSE;
    }
    $output .= theme('pager', NULL, 5);
    $node = array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''),
		  'type' => 'pentry', 'pid'=>$node->nid, 'language' => ''); 
    $output .= drupal_get_form('pentry_node_form', $node);
  return $output;
}


function pdiary_page_diary($uid, $pid, $nid) {
  return ;
}

function pdiary_add() {
  global $user;

  $types = get_node_types();
  if (!isset($types['pdiary']) || !node_access('create', 'pdiary')) {
    return;
  }
  return node_add('pdiary');

  
}

?>
